package com.dmall.play.interview.chapter07;

public class Leetcode100 {

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {}

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q == null) {
            return true;
        } else if (p == null) {
            return false;
        } else if (q == null) {
            return false;
        } else if (p.val != q.val) {
            return false;
        } else {
            // else 表示 p 和 q 的 val 相等
            boolean sameLeft = isSameTree(p.left, q.left);
            boolean sameRight = isSameTree(p.right, q.right);
            return sameLeft && sameRight;
        }
    }

    public static void main(String[] args) {
        TreeNode p = new TreeNode(1);
        p.left = new TreeNode(2);
        p.right = new TreeNode(3);

        TreeNode q = new TreeNode(1);
        q.left = new TreeNode(2);
        q.right = new TreeNode(3);

        Leetcode100 leetcode100 = new Leetcode100();
        System.out.println(leetcode100.isSameTree(p, q));
    }
}
